<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 陈风任 <491085389@qq.com>
 * Date: 2019-03-26
 */

namespace app\admin\controller;

use think\Db;
use think\Page;
use think\Config;

class ShopService extends Base {

    private $UsersConfigData = [];

    /**
     * 构造方法
     */
    public function __construct(){
        parent::__construct();
        // 售后服务数据表(维权订单数据表)
        $this->shopOrderServiceDb = Db::name('shop_order_service');
        // 售后服务数据模型(维权订单数据模型)
        $this->shopOrderServiceModel = model('ShopOrderService');
    }

    // 退换货服务数据列表
    public function after_service()
    {
        $param = input('param.');
        $this->assign('param', $param);

        // 获取退换货服务信息
        $result = $this->shopOrderServiceModel->getOrderServiceList();
        $this->assign('page', $result['pageStr']);
        $this->assign('pager', $result['pageObj']);
        $this->assign('serviceList', $result['serviceList']);
        // 维权订单状态
        $this->assign('serviceStatus', Config::get('global.order_service_status'));
        // 主体订单状态
        $this->assign('adminOrderStatus', Config::get('global.admin_order_status_arr'));

        return $this->fetch('after_service');
    }

    // 退换货服务数据详情
    public function after_service_details()
    {
        $service_id = input('param.service_id/d', 0);
        if (!empty($service_id)) {
            // 查询服务信息
            $result = $this->shopOrderServiceModel->getOrderServiceDetails($service_id);
            if (empty($result)) $this->error('维权订单不存在');

            // 上一个页面来源
            $backurl = input('param.backurl/s');
            $result['backurl'] = $backurl;

            // 是否弹窗打开
            $iframe = input('param.iframe/d',0);
            $result['iframe'] = $iframe;
            $this->assign($result);
            
            return $this->fetch('after_service_details');
        } else {
            $this->error('非法访问！');
        }
    }

    // 更新退换货信息
    public function after_service_handle()
    {
        if (IS_AJAX_POST) {
            $post = input('post.');
            if (empty($post['status']) || empty($post['service_id'])) $this->error('请选择审核意见！');
            if (empty($post['users_id']) || empty($post['order_id']) || empty($post['details_id'])) $this->error('数据错误，刷新重试！');

            // 更新服务单数据
            $this->shopOrderServiceModel->afterServiceHandle($post);
        }
    }

    // 退款页面信息
    public function after_service_refund()
    {
        // 查询维权订单信息
        $serviceID = input('param.service_id/d', 0);
        $result = $this->shopOrderServiceModel->getOrderServiceDetails($serviceID);
        if (empty($result)) $this->error('维权订单不存在');
        $this->assign($result);

        return $this->fetch();
    }

    // 维权订单重新发货
    public function after_service_resend()
    {
        // 查询维权订单信息
        $serviceID = input('param.service_id/d', 0);
        $result = $this->shopOrderServiceModel->getOrderServiceDetails($serviceID);
        if (empty($result)) $this->error('维权订单不存在');
        $this->assign($result);

        $where = [
            'is_choose' => 1,
        ];
        $express = Db::name('shop_express')->where($where)->order('sort_order asc, express_id asc')->select();
        $this->assign('express', $express);

        return $this->fetch();
    }

    // 退换货服务数据删除
    public function after_service_del()
    {
        $service_id = input('del_id/a');
        $service_id = eyIntval($service_id);
        if (IS_AJAX_POST && !empty($service_id)) {
            // 条件数组
            $Where = [
                'service_id' => ['IN', $service_id]
            ];
            // 查询数据
            $result = $this->shopOrderServiceDb->field('order_code')->where($Where)->select();
            $order_code_list = get_arr_column($result, 'order_code');

            // 删除数据
            $ResultID = $this->shopOrderServiceDb->where($Where)->delete();
            if (!empty($ResultID)) {
                // 同步删除订单下的操作记录
                Db::name('shop_order_service_log')->where($Where)->delete();

                // 存在adminlog日志
                adminLog('删除订单：'.implode(',', $order_code_list));
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }
        $this->error('参数有误');
    }

    // 会员编辑 退货数据列表
    public function users_edit_after_service()
    {
        $param = input('param.');
        $this->assign('param', $param);

        // 获取退换货服务信息
        $result = $this->shopOrderServiceModel->getUsersOrderServiceList(false,$param['users_id']);
        $this->assign('page', $result['pageStr']);
        $this->assign('pager', $result['pageObj']);
        $this->assign('serviceList', $result['serviceList']);
        // 主体订单状态
        $this->assign('adminOrderStatus', Config::get('global.admin_order_status_arr'));

        return $this->fetch('member/edit/refund_index');
    }
}